package com.huxiu.holder;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huxiu.DetailActivity;
import com.huxiu.R;
import com.huxiu.net.model.TuiJianItem;
import com.huxiu.utils.Constants;
import com.huxiu.utils.ImageLoaderOptions;
import com.huxiu.utils.UmenEvent;
import com.huxiu.utils.Utils;
import com.huxiu.view.RefreshListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

import java.lang.ref.WeakReference;


public class TuiJianHolder implements OnClickListener {

    @ViewInject(R.id.home_item_img)
    private ImageView imgage;

    @ViewInject(R.id.home_item_jianbian)
    private ImageView jianbian;

    @ViewInject(R.id.home_item_no_tuiguang)
    private LinearLayout noTuiGuang;

    @ViewInject(R.id.home_item_name)
    private TextView name;
    @ViewInject(R.id.home_item_time)
    private TextView time;

    @ViewInject(R.id.home_item_zan_num)
    private TextView zanNum;
    @ViewInject(R.id.home_item_pinglun_num)
    private TextView pingLunNum;

    @ViewInject(R.id.home_item_zhuanti)
    private TextView zhuanti;
    @ViewInject(R.id.home_item_tuijian)
    private TextView tuijian;

    @ViewInject(R.id.home_item_tuiguang)
    private TextView tuiguang;

    @ViewInject(R.id.home_item_title)
    private TextView title;


    private static Activity mContext;
    private int position = -1;
    private RefreshListView lv_tabdetail_list;
    private static WeakReference<BaseAdapter> mAdapter;

    public void setAdapter(BaseAdapter adapter) {
        mAdapter = new WeakReference<>(adapter);
    }

    public TuiJianHolder(View view, Context context) {
        ViewUtils.inject(this, view);
        mContext = (Activity) context;
        title.setOnClickListener(this);
        imgage.setOnClickListener(this);
    }

    public TuiJianHolder(View view, Context context, int position) {
        ViewUtils.inject(this, view);
        mContext = (Activity) context;
        this.position = position;
        title.setOnClickListener(this);
        imgage.setOnClickListener(this);
    }

    //    private boolean isMine2 = false;

    private TuiJianItem mItem;

    public void bindItem(TuiJianItem item) {
        mItem = item;
        String times;
        switch (mItem.article_type) {
            case 1:
                noTuiGuang.setVisibility(View.VISIBLE);
                tuijian.setVisibility(View.GONE);
                tuiguang.setVisibility(View.GONE);
                zhuanti.setVisibility(View.GONE);
                jianbian.setVisibility(View.VISIBLE);
                times = Utils.getDateString(mItem.dateline);
                time.setText(times);
                zanNum.setText(Integer.toString(mItem.agreenum));
                pingLunNum.setText(Integer.toString(mItem.commentnum));
                name.setText(mItem.author);
                break;
            case 2:
                noTuiGuang.setVisibility(View.VISIBLE);
                zhuanti.setVisibility(View.GONE);
                tuiguang.setVisibility(View.GONE);
                tuijian.setVisibility(View.VISIBLE);
                jianbian.setVisibility(View.VISIBLE);
                times = Utils.getDateString(mItem.dateline);
                time.setText(times);
                zanNum.setText(Integer.toString(mItem.agreenum));
                pingLunNum.setText(Integer.toString(mItem.commentnum));
                name.setText(mItem.author);
                break;
            case 3:
                noTuiGuang.setVisibility(View.INVISIBLE);
                tuiguang.setVisibility(View.VISIBLE);
                tuiguang.setText("赞助");
                jianbian.setVisibility(View.VISIBLE);
                break;
            case 4:
                noTuiGuang.setVisibility(View.INVISIBLE);
                tuiguang.setVisibility(View.VISIBLE);
                tuiguang.setText("广告");
                jianbian.setVisibility(View.VISIBLE);
                break;
            case 5:
                noTuiGuang.setVisibility(View.VISIBLE);
                zhuanti.setVisibility(View.VISIBLE);
                tuiguang.setVisibility(View.GONE);
                tuijian.setVisibility(View.GONE);
                jianbian.setVisibility(View.VISIBLE);
                times = Utils.getDateString(mItem.dateline);
                time.setText(times);
                zanNum.setText(Integer.toString(mItem.agreenum));
                pingLunNum.setText(Integer.toString(mItem.commentnum));
                name.setText(mItem.author);
                break;
            case 6:
                noTuiGuang.setVisibility(View.INVISIBLE);
                tuiguang.setVisibility(View.VISIBLE);
                tuiguang.setText("推广");
                jianbian.setVisibility(View.VISIBLE);
                break;
            default:
                noTuiGuang.setVisibility(View.INVISIBLE);
                tuiguang.setVisibility(View.VISIBLE);
                tuiguang.setText("特别策划");
                jianbian.setVisibility(View.VISIBLE);
                break;
        }
        title.setText(mItem.title);
        ImageLoader.getInstance().displayImage(
                mItem.pic_path + Constants.IMAGE_WEBP, imgage, ImageLoaderOptions.options);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_item_title:
            case R.id.home_item_img:
                Intent intent = new Intent(mContext, DetailActivity.class);
                intent.putExtra(Constants.TUIJIAN_ITEM_KEY, mItem.toString());
                mContext.startActivity(intent);
                UmenEvent.UmengEventMap(mContext, UmenEvent.APP_HOME, UmenEvent.HOME_ARTICLELIST_CLICK);
                break;
        }
    }

}
